package proxy
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	import org.aswing.Container;
	import org.aswing.JButton;
	import org.aswing.JTextArea;
	import org.aswing.JTextComponent;
	import org.aswing.JTextField;
	
	public class RemoteProxyExample extends Container
	{
		private var flickr:PhotoSearchProxy;
		private var btn:JButton;
		private var searchTxt:JTextField;
		private var xmlTxt:JTextArea;
		private var nextBtn:JButton;
		private var xml:XML;
		private var loader:Loader;
		private var num:int;
		public function RemoteProxyExample()
		{
			this.width = 1000;
			this.height = 600;
			init();
		}
		private function init():void{
			flickr = new PhotoSearchProxy;
			flickr.addEventListener(Event.COMPLETE,onComplete);
			
			btn = new JButton("搜索");
			btn.width = 100;
			btn.height = 30;
			btn.x = 500;
			this.addChild(btn);
			btn.addEventListener(MouseEvent.CLICK,onSearch);
			
			nextBtn = new JButton("下一个");
			nextBtn.width = 100;
			nextBtn.height = 30;
			nextBtn.x = 750;
			this.addChild(nextBtn);
			nextBtn.addEventListener(MouseEvent.CLICK,onNext)
			
			searchTxt = new JTextField();
			searchTxt.width = 100;
			searchTxt.height = 30;
			searchTxt.x = 620;
			this.addChild(searchTxt);
			
			xmlTxt = new JTextArea;
			xmlTxt.width = 400;
			xmlTxt.height = 400;
			xmlTxt.x = 500;
			xmlTxt.y = 100;
			this.addChild(xmlTxt);
		}
		private function onSearch(event:MouseEvent):void{
			flickr.search("",searchTxt.getText());
		}
		private function onNext(event:MouseEvent):void{
			num++;
			setPic(num);
		}
		private function onComplete(event:DataEvent):void{
			//trace(event.data);
			//http://farm5.static.flickr.com/4102/4887601035_5754dde398_z.jpg
			xml= XML(event.data);
			xmlTxt.setText(xml.toString());
			loader = new Loader;
			this.addChild(loader);
			setPic(0);
		}
		private function setPic(num:int):void{
			var i:int = num;
			//xml= XML(event.data);
			//xmlTxt.setText(xml.toString())
			var url:URLRequest = new URLRequest(
				"http://farm" + xml.photos.photo[i].@farm + ".static.flickr.com/" + xml.photos.photo[i].@server 
				+ "/" + xml.photos.photo[i].@id + "_" + xml.photos.photo[i].@secret + ".jpg"
			);
			trace(url.url)
			loader.load(url);
		}
		
	}
}